//
//  RNoteInfoViewController.m
//  Riji
//
//  Created by  on 2022/8/20.
//

#import "RNoteInfoViewController.h"
#import "RPhotoCell.h"
#import "REditNoteViewController.h"
#import "RNoteCell.h"
@interface RNoteInfoViewController ()
@property (strong, nonatomic) IBOutlet UILabel *date;
@property (strong, nonatomic) IBOutlet UILabel *time;
@property (strong, nonatomic) IBOutlet UIButton *address;
@property (strong, nonatomic) IBOutlet UIButton *weather;
@property (strong, nonatomic) IBOutlet UIButton *feeling;
@property (strong, nonatomic) IBOutlet UIButton *numBtn;
@property (strong, nonatomic) IBOutlet UILabel *content;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *moreBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewH;
@property (strong, nonatomic) IBOutlet UIButton *leftBtn;
@property (strong, nonatomic) IBOutlet UIButton *rightBtn;
@property (nonatomic, strong) RNoteModel *model;
@end

@implementation RNoteInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:notifyNoteUpdate object:nil];
}
- (void)initUI{
    self.title = self.type == NotesTypeNormal ? LS(@"日记详情") : LS(@"回收站日记详情");
    [self.collectionView registerNib:[RPhotoCell nib] forCellWithReuseIdentifier:[RPhotoCell ID]];
    self.moreBtn.hidden = NO;
}
- (UIButton *)moreBtn{
    if (!_moreBtn) {
        _moreBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, StuBarHight + 5, 40, 40)];
        [_moreBtn setTitle:LS(@"更多")forState:UIControlStateNormal];
        [_moreBtn addTarget:self action:@selector(actionFormoreBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.moreBtn];
    }
    return _moreBtn;
}
- (void)setModel:(RNoteModel *)model{
    _model = model;
    self.date.text = [NSDate transToDate:@"yyyy-MM-dd" :self.model.stamp];
    self.time.text = [NSString stringWithFormat:@"%@ %@",[NSDate transToDate:@"HH:mm" :self.model.stamp],LS([RNoteCell week:[self.model.week intValue]])];
    [self.address setTitle:self.model.address forState:UIControlStateNormal];
    [self.weather setTitle:LS(self.model.weather) forState:UIControlStateNormal];
    [self.feeling setTitle:self.model.feeling forState:UIControlStateNormal];
    self.content.text = self.model.content;
    [self.numBtn setTitle:[NSString stringWithFormat:LS(@"%d字"),(int)self.content.text.length] forState:UIControlStateNormal];
    self.leftBtn.hidden = self.array.firstObject == _model || self.type == NotesTypeRecycle;
    self.rightBtn.hidden = self.array.lastObject == _model || self.type == NotesTypeRecycle;
    [self.collectionView reloadData];
}
- (void)loadData{
    if (self.array.count > 0) {
        self.model = self.array[self.index];
    }
}
#pragma mark - 更多按钮方法
-(void)actionFormoreBtn:(UIButton*)sender{
    NSArray *array = self.type == NotesTypeNormal ? @[@"编辑",@"分享给好友",@"删除"] : @[@"详情",@"移出回收站",@"彻底删除"];
    [self sheet:nil msg:nil sheet:array sheetBlock:^(int index, NSString *sheetTitle) {
        if ([sheetTitle isEqualToString:@"详情"]) {
            NSString *form = @"yyyy-MM-dd hh:mm";
            NSString*str1 = [NSDate transToDate:form :self.model.stamp];
            NSString*str2 = [NSDate transToDate:form :self.model.updateStamp];
            NSString *str3 = [NSString stringWithFormat:@"%@: %@ \n%@: %@",LS(@"记录时间"),str1,LS(@"删除时间"),str2];
            RPopAlertViewController *vc = [RPopAlertViewController new];
            vc.msgLbl.text = str3;
            vc.enterBtn.hidden = YES;
            vc.enterBlock = ^(NSString * _Nonnull string) {
                
            };
            [self presentViewController:vc animated:NO completion:nil];
        }else if ([sheetTitle isEqualToString:@"移出回收站"]){
            self.model.remove = NO;
            [self.model bg_saveOrUpdate];
            [[NSNotificationCenter defaultCenter] postNotificationName:notifyNoteUpdate object:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else if ([sheetTitle isEqualToString:@"彻底删除"]){
            NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"stamp"),bg_sqlValue(@(self.model.stamp))];
            [RNoteModel bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {}];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else if ([sheetTitle isEqualToString:@"编辑"]){
            REditNoteViewController *vc = [REditNoteViewController new];
            vc.model = self.model;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([sheetTitle isEqualToString:@"分享给好友"]){
            UIImage *img = self.model.data != nil && [self.model.data count] > 0 ? [UIImage imageWithData:self.model.data[0]] : [UIImage imageNamed:@""];
            [Tool share:@[self.model.content,img] target:self success:^(BOOL success, NSString * _Nonnull result) {
                
            }];
        }else if ([sheetTitle isEqualToString:@"删除"]){
            RPopAlertViewController *vc = [RPopAlertViewController new];
            vc.msgLbl.text = LS(@"真的要删除这篇日记吗？");
            vc.enterBlock = ^(NSString * _Nonnull string) {
                self.model.remove = YES;
                self.model.updateStamp = [[NSDate currentTimeStamp:@"yyyy/MM/dd hh:mm"] intValue];
                [self.model bg_saveOrUpdate];
                [[NSNotificationCenter defaultCenter] postNotificationName:notifyNoteUpdate object:nil];
                [self.navigationController popToRootViewControllerAnimated:YES];
            };
            [self presentViewController:vc animated:NO completion:nil];
        }
    }];
}
#pragma mark - 上一篇/下一篇按钮方法
- (IBAction)actionForBtn:(UIButton *)sender {
    self.index = sender.tag == 201 ? self.index - 1 : self.index + 1;
    self.model = self.array[self.index];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.model.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RPhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RPhotoCell ID] forIndexPath:indexPath];
    cell.img.image = [UIImage imageWithData:self.model.data[indexPath.row]];
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 15 * 4) / 3;
    int row = ceil(10 / 3.0);
    self.collectionViewH.constant = w * row + 15 * (row - 1);
    return  CGSizeMake(w,w);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

@end
